package com.leyou.cart.interceptor;

import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.constants.LyConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class CartInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userIdStr = request.getHeader(LyConstants.USER_HOLDER_KEY);
        //判空
        if(StringUtils.isEmpty(userIdStr)){
            log.error("【购物车微服务校验来访用户是否登录】未登录！");
            return false;
        }
        //获取用户id
        Long userId = Long.valueOf(userIdStr);
        //把用户id放入线程的局部变量中
        UserHolder.setUserId(userId);
        return true;
    }

}
